import Vue from 'vue'
const PaginateStore = {
  state () {
    return {
      paginate: {
        paging: {
          currentPage: '',
          totalItem: '',
          totalPage: '',
          pageSize: ''
        }
      },
      queryPaginate: {
        currentPage: 1,
        pageSize: 10
      }
    }
  },

  getters: {
    getPaginate: (state) => (id) => {
      if (!state.paginate[id]) {
        Vue.set(state.paginate, id, {
          currentPage: '',
          totalItem: '',
          totalPage: '',
          pageSize: ''
        })
      }
      return state.paginate
    },
    getQueryPaginate: state => {
      return state.queryPaginate
    }
  },
  mutations: {
    muPaginate (state, data) {
      const query = data.query
      if (!query) {
        return
      }
      let pagingId = data.pagingId
      if (!pagingId) {
        pagingId = 'paging'
      }
      let pageNum = ''
      let total = ''
      let pages = ''
      if (query && query.pageList) {
        pageNum = query.pageList.pageNum
        total = query.pageList.total
        pages = query.pageList.pages
      }
      const obj = {
        currentPage: query.currentPage || query.pageNum || pageNum,
        totalItem: query.totalItem || query.total || total,
        totalPage: query.totalPage || query.pages || pages,
        pageSize: query.pageSize || query.pageList.pageSize
      }
      Vue.set(state.paginate, pagingId, obj)
    },
    muQueryPaginate (state, query) {
      state.queryPaginate.currentPage = query.currentPage
      state.queryPaginate.pageSize = query.pageSize
    },
    removeQueryPaginate (state) {
      state.queryPaginate.currentPage = 1
      // state.queryPaginate.pageSize = 10
    }
  }
}

export default PaginateStore
